package com.example.service.impl;

import com.example.pojo.dto.UserLoginInfo;
import com.example.service.UserLoginInfoService;
import com.mongodb.client.result.UpdateResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Service;

/**
 * @Author: hualao
 * @Version: 0.0.1V
 * @Date: 2019/1/11
 * @Description: 用户登录消息实现类
 **/
@Service
public class UserLoginInfoServiceImpl implements UserLoginInfoService {

    @Autowired
    MongoTemplate mongoTemplate;

    @Override
    public UserLoginInfo saveUserLoginInfo(UserLoginInfo info) {
        mongoTemplate.save(info);
        return info;
    }

    @Override
    public UserLoginInfo findUserLoginInfoUserId(Integer userId) {
        Query query = new Query(Criteria.where("userId").is(userId));
        return mongoTemplate.findOne(query, UserLoginInfo.class);
    }

    @Override
    public Long updateUserLoginInfoById(String id, String username) {
        Query query = new Query(Criteria.where("id").is(id));
        Update update = Update.update("username", username);
        UpdateResult updateResult = mongoTemplate.updateMulti(query, update, UserLoginInfo.class);
        return updateResult.getMatchedCount();
    }

    @Override
    public UserLoginInfo delUserLoginInfoById(String id) {
        Query query = new Query(Criteria.where("_id").is(id));
        return mongoTemplate.findAndRemove(query, UserLoginInfo.class);
    }


}
